const brand = require("express").Router()

const {rules, valid} = require("../../utils/rules");

const sqlOptions = require("../../sql");

// const handlerList = require("../../handler/handlerList");

const nanoid = require("nanoid").nanoid

const brandSave = [
    {
        name: "category_id",
        message: "分类id"
    },
    {
        name: "brand_name",
        message: "品牌名称"
    },
]

const brandUpdate = [].concat(brandSave, [
    {
        name: "brand_id",
        message: "品牌名称"
    },
])

// 添加
brand.post("/save", rules(brandSave), valid, async (req, res) => {
    try {
        const data = req.body

        const category_ids = data.category_id.split(",")
        const list = category_ids.map(item => {
            return {
                category_id: item,
                brand_name: data.brand_name,
                brand_id: nanoid()
            }
        })
        const sqlSave = sqlOptions({
            name: "sms_brand",
            method: "save",
            data: list
        })

        const sqlList = sqlOptions({
            name: "sms_brand",
            method: "list",
            data: {
                brand_name: data.brand_name
            }
        })
        // console.log(sqlSave)

        const rows = await req.db.query(sqlList)
        if (rows.length) {
            //category_ids
            const cates = rows.filter(item => category_ids.includes(item.category_id) )
            if (cates.length) {
                return res.status(200).send({
                    code: 400,
                    message: "分类名称重复无法添加",
                    data: ""
                })
            }
        }
        await req.db.query(sqlSave)
        return res.status(200).send({
            code: 200,
            message: "添加成功",
            data: ""
        })


    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})
// 查询  需要修改
/*brand.post("/list", async (req, res) => handlerList(req, res, ""))*/

brand.post("/list", async (req, res) => {
    try {
        const {current, pageSize, data} = req.body
        const [total] = await req.db.query(sqlOptions({
            method: "total",
            name: "sms_brand"
        }))
        console.log(total)
        const list = await req.db.query(sqlOptions({
            name: "sms_brand",
            method: "list",
            data,
            current,
            pageSize
        }))
        const categoryLIst = await req.db.query(sqlOptions({
            name: "sms_category",
            method: "list",
            data: {}
        }))
        return res.status(200).send({
            code: 200,
            message: "",
            data: {
                current: current,
                pageSize: pageSize,
                list: list.map(item => {
                    const nameStr = categoryLIst.filter(i => i.category_id === item.category_id)[0]
                    return {
                        ...item,
                        category_name: nameStr.category_name
                    }
                }),
                total: total["COUNT(*)"]
            }
        })
    }catch (e) {
        console.log(e)
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})
// 编辑
brand.post("/update", rules(brandUpdate), valid, async (req, res) => {
    try {
        const {
            category_id,
            brand_name,
            brand_id
        } = req.body

        const sqlList = sqlOptions({
            name: 'sms_brand',
            method: "list",
            data: {
                brand_id,
            }
        })

        const list = await req.db.query(sqlList)
        if (list.length) {
            const category = await req.db.query(sqlOptions({
                name: 'sms_brand',
                method: "list",
                if: "AND",
                data: {
                    category_id,
                    brand_name
                }
            }))
            if (category.length === 0) {
                const sqlUpdate = sqlOptions({
                    name: "sms_brand",
                    method: 'update',
                    key: {
                        brand_id
                    },
                    data: {
                        category_id,
                        brand_name,
                    }
                })
                await req.db.query(sqlUpdate)
                return res.status(200).send({
                    code: 200,
                    message: "编辑成功",
                    data: ''
                })
            }
            if (category[0].category_id === list[0].category_id) {
                return res.status(200).send({
                    code: 200,
                    message: "编辑成功",
                    data: ''
                })
            }
            return res.status(200).send({
                code: 400,
                message: "编辑失败, 品牌名称重复",
                data: ''
            })
        }
        return res.status(200).send({
            code: 400,
            message: "编辑失败, 列表中没有这条数据",
            data: ''
        })
    } catch (e) {
        console.log(e)
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})
// 删除
brand.delete("/delete", async (req, res) => {
// 需要删除品牌表以及商品表和推荐表
    try {
        const { brand_id } = req.query
        /*const brandSql = sqlOptions({
            name: "sms_brand",
            method: "delete",
            data: {
                brand_id
            }
        })
        const productSql = sqlOptions({
            method: 'delete',
            name: 'sms_product',
            data: {
                brand_id
            }
        })
        const recommendSql = sqlOptions({
            method: 'delete',
            name: 'sms_recommend',
            data: {
                brand_id
            }
        })*/

        /*await req.db.query(brandSql)
        await req.db.query(productSql)
        await req.db.query(recommendSql)*/

        await req.db.query(sqlOptions({
            method: "procedure",
            procedure: "brand",
            value: brand_id
        }))
        return res.status(200).send({
            code: 200,
            message: '删除成功',
            data: ''
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})

brand.post("/queryList", async (req, res) => {
    try {
        console.log(req.body)
        const list = await req.db.query(sqlOptions({
            name: "sms_brand",
            method: "list",
            data: req.body,
        }))
        return res.status(200).send({
            code: 200,
            message: '',
            data: list
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})


module.exports = brand